package com.neusoft.elm.view.Impl;

import java.util.List;
import java.util.Scanner;

import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.dao.impl.FoodDaoImpl;
import com.neusoft.elm.po.Food;

public class FoodViewImpl implements com.neusoft.elm.view.FoodView {

	private Scanner input = new Scanner(System.in);
	
	@Override
	public List<Food> showFoodList(Integer businessId) {

		FoodDao dao = new FoodDaoImpl();
		List<Food> list = dao.listFoodByBusinessId(businessId);
		
		if(list.size() == 0){
			System.out.println("没有食品信息");
		}else{
			System.out.println("食品编号\t食品名称\t食品介绍\t食品价格");
			
			for(Food f: list){
				System.out.println(f.getFoodId()+"\t"+f.getFoodName()+"\t"+f.getFoodExplain()+"\t"+f.getFoodPrice());
			}
		}
		
		return list;
	}

	@Override
	public void saveFood(Integer businessId) {
		
		int result = -1;
		
		Food food = new Food();
		FoodDao dao = new FoodDaoImpl();
		
		System.out.println("请输入食品名称");
		food.setFoodName(input.next());
		System.out.println("请输入食品介绍");
		food.setFoodExplain(input.next());
		System.out.println("请输入食品价格");
		food.setFoodPrice(input.nextDouble());
		food.setBusinessId(businessId);
		
		result = dao.saveFood(food);
		
		if(result > 0){
			System.out.println("新建食品成功");
		}else{
			System.out.println("新建食品失败");
		}
	}
	
	public void editFood(Integer businessId) {
	    Food food = null;
	    FoodDao dao = new FoodDaoImpl();
	    List<Food> list = showFoodList(businessId);
	    String inputStr = "";
	    int foodIdValue ;
	    Scanner input = new Scanner(System.in); 
	    
	    if(list.size() != 0){
	    	System.out.println("请输入你想修改食品的食品编号");
		    foodIdValue = input.nextInt();
		    
//		    增强for循环取出list中的指定food对象
//		    for(Food f : list){
//		    	if(f.getFoodId() == foodIdValue){
//		    		food = f;
//		    	}
//		    }

//		    getFood方法
		    food = dao.getFoodByFoodId(foodIdValue);
		    System.out.println(food.toString());;
		    
		    System.out.println("是否修改食品名称？(y/n)");
		    inputStr = input.next();
		    if(inputStr.equalsIgnoreCase("y")){
		        System.out.println("请输入新的食品名称");
		        food.setFoodName(input.next());
		    }

		    System.out.println("是否修改食品介绍？(y/n)");
		    inputStr = input.next();
		    if(inputStr.equalsIgnoreCase("y")){
		        System.out.println("请输入新的食品介绍");
		        food.setFoodExplain(input.next()); 
		    }

		    System.out.println("是否修改食品价格？(y/n)");
		    inputStr = input.next();
		    if(inputStr.equalsIgnoreCase("y")){
		        System.out.println("请输入新的食品价格");
		        food.setFoodPrice(input.nextDouble());
		    }

//		    System.out.println("是否修改所属商家？(y/n)");
//		    inputStr = input.next();
//		    if(inputStr.equalsIgnoreCase("y")){
//		        System.out.println("请输入新的商家ID");
//		        food.setBusinessId(input.nextInt());
//		    }

		    int result = dao.updateFood(food);

		    if(result > 0){
		        System.out.println("食品信息修改成功");
		    } else {
		        System.out.println("食品信息修改失败");
		    }
	    }
	}

	@Override
	public void removeFood(Integer businessId) {
		FoodDao dao = new FoodDaoImpl();
	    List<Food> list = showFoodList(businessId);
	    int foodIdValue ;
	    Scanner input = new Scanner(System.in); 
		int result = -1;
		String inputStr = "";
		
	    if(list.size() == 0){
	    	System.out.println("没有食品信息");
	    }else {
	    	System.out.println("请选择你要删除食品的食品编号");
	    	foodIdValue = input.nextInt();
	    	
	    	System.out.println("你确认要删除这条数据吗？(y/n)");
	    	inputStr = input.next();
	    	if(inputStr.equals("y")){
	    		result = dao.removeFood(foodIdValue);
	    		if(result > 0 ){
		    		System.out.println("食品信息删除成功");
		    	}else{
		    		System.out.println("食品信息删除失败");
		    	}
	    	}else{
	    		System.out.println("放弃删除");
	    	}
	    }
	    
	}

}
